package com.sfac.javaSpringBoot.modules.account.dao;

import com.sfac.javaSpringBoot.modules.account.entity.Role;
import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RoleDao {
    @Insert("insert into account_role (create_date,role_name)" +
            "values (#{createDate},#{roleName})")
    /*插入完成时拿到roleId*/
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertRole(Role role);

    @Update("update account_role set role_name=#{roleName}" +
            "where id=#{id}")
    void updateRole(Role role);

    @Delete("delete from account_role where id=#{id}")
    void deleteRoleById(int id);

    @Select("select * from account_role ar left join account_user_role aur on ar.id = aur.role_id "
            + "where aur.user_id = #{userId}")
    Role queryRoleById(int id);

    @Select("select * from account_role ar left join account_user_role aur on ar.id = aur.role_id "
            + "where aur.user_id = #{userId}")
    List<Role> getRolesByUserId(int userId);

    @Select("select * from account_role ar left join account_role_resource arr on ar.id = arr.role_id "
            + "where arr.resource_id = #{resourceId}")
    List<Role> getRolesByResourceId(int resourceId);

    @Select("select * from account_role")
    List<Role> queryRoleAll();

    @Select("<script>"
            + "select * from account_role "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (role_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Role> getRolesBySearchBean(SearchBean searchBean);
}
